package netapp.tcp_3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        // 创建服务器对象
        ServerSocket ss = new ServerSocket(6666);
        Socket accept = ss.accept();

        // 二、读取客户端发送的数据
        BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
        // 将获取的字节数据写入到文件中
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("AdvancedCode/dest/copy.jpg"));

        byte[] bytes = new byte[1024 * 10];
        int count;
        while ((count = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, count);
            bos.flush();
        }

        // 关闭本地输出流
        bos.close();
        // 禁用socket输入流
        accept.shutdownInput();


        // 三、向客户端写出反馈数据
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        bw.write("上传成功");
        bw.flush();

        // 关闭客户端
        accept.close();
        // 关闭服务端
        ss.close();
    }
}
